package com.study.flink.batch;

import org.apache.flink.api.common.JobExecutionResult;
import org.apache.flink.api.common.accumulators.IntCounter;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.operators.MapOperator;
import org.apache.flink.configuration.Configuration;

import java.util.ArrayList;

/**
 * 全局累加器（Accumulators）
 * @author stephen
 * @create 2019-01-28 21:05
 * @since 1.0.0
 */
public class BatchDemoCounter {

    public static void main(String[] args) throws Exception {
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

        ArrayList<String> data = new ArrayList<>();
        data.add("hello world");
        data.add("hello china");
        data.add("hello china");
        data.add("hello china");
        data.add("hello china");
        data.add("hello china");
        data.add("hello china");

        DataSource<String> source = env.fromCollection(data);

        MapOperator<String, String> result = source.map(new RichMapFunction<String, String>() {

            // 创建累加器
            IntCounter counter = new IntCounter();

            @Override
            public void open(Configuration parameters) throws Exception {
                super.open(parameters);
                // 注册了累加器
                getRuntimeContext().addAccumulator("num-lines", counter);
            }

            @Override
            public String map(String value) throws Exception {
                this.counter.add(1);
                return value;
            }
        }).setParallelism(2);

        result.writeAsText("./counter");
        // 任务结束后才能获取结果
        JobExecutionResult jobResult = env.execute("counter");
        Integer counter = jobResult.getAccumulatorResult("num-lines");

        System.out.println("counter: "+counter);
    }
}
